<?php

namespace mpend\modules\team\models;

use Yii;
use common\helpers\Url;
use common\models\workbook\Workbook;

/**
 * 记工日历面板信息
 *
 * @author emhome <emhome@163.com>
 * @since 1.0
 */
class BookDashboardLogic extends \common\logics\DashboardLogic {

    /**
     * @var string|date 开始时间
     */
    public $workbook_id;

    /**
     * @var Workbook 记工表名称
     */
    public $workbook;

    /**
     * @var string|date 开始时间
     */
    public $date;

    /**
     * @var array top pages
     */
    public $date_stats = [];

    /**
     * @var array top pages
     */
    public $workers = [];

    /**
     * @var array top pages
     */
    public $shareData;

    /**
     * Set Dual column display panel
     * @inheritdoc
     */
    protected function setWorkbook() {
        return Workbook::find()->active(Workbook::STATUS_ACTIVE)->andWhere([
            'id' => $this->workbook_id
        ])->one();
    }

    /**
     * Set Dual column display panel
     * @inheritdoc
     */
    protected function setDateStats() {
        $model = new TeamBookDateStatsSearch();
        $model->workbook_id = $this->workbook_id;
        $model->begin_date = $this->date;
        $model->pagesize = false;
        return $model->search();
    }

    /**
     * Set top pages
     * @inheritdoc
     */
    protected function setWorkers() {
        $model = new TeamBookMemberDateSearch();
        $model->pagesize = false;
        $model->workbook_id = $this->workbook_id;
        $model->setExtraAttributes([
            'date' => $this->date
        ]);
        return $model->search();
    }

    /**
     * Set top pages
     * @inheritdoc
     */
    protected function setShareData() {
        $user = $this->getUser();
        if (!$user) {
            return null;
        }
        return [
            'title' => Yii::t('message', '{name} 邀请你加入班组记工', [
                'name' => $user->getUserName()
            ]),
            'path' => Url::to(['/pages/share/main', 'workbook_id' => $this->workbook_id, 'from_user_id' => $user->id, 'type' => 'join']),
            'imageUrl' => 'https://oss.youmbe.com/assets/share/foreman.jpg',
        ];
    }

}
